package cn.rxls.community.controller;

import cn.rxls.community.dto.bill.AddBillDTO;
import cn.rxls.community.dto.bill.PageBillCategoryDTO;
import cn.rxls.community.enums.ResultEnum;
import cn.rxls.community.exception.BusinessException;
import cn.rxls.community.result.Result;
import cn.rxls.community.service.BillService;
import cn.rxls.community.strategy.bill.BillStrategy;
import cn.rxls.community.strategy.bill.BillStrategyFactory;
import cn.rxls.community.vo.bill.BillVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

/**
 * 账单(Bill)表
 */
@RestController
@RequestMapping("bill")
@Valid
public class BillController {
    /**
     * 服务对象
     */
    @Resource
    private BillService billService;

    /**
     * 根据分类分页查询账单
     *
     * @param dto 参数对象
     * @return 多条账单信息
     */
    @GetMapping("/page/category")
    public Result<List<BillVO>> selectByCategory(@Valid @ModelAttribute PageBillCategoryDTO dto) {
        return Result.success(billService.selectPageBillCategory(dto));
    }

    /**
     * 账单创建
     *
     * @param dto 参数对象
     * @return 是否成功
     */
    @PostMapping("/add")
    public Result<Boolean> addBillByCategory(@RequestBody AddBillDTO dto) {
        BillStrategy strategy = BillStrategyFactory.getStrategy(dto.getBillCategory());
        Optional.ofNullable(strategy).orElseThrow(() -> new BusinessException(ResultEnum.ERROR_TYPE_BILL));
        return Result.success(strategy.addBill(dto));
    }

    /**
     * 根据id查询账单
     *
     * @param id 账单id
     * @return 单条信息
     */
    @GetMapping("/detail")
    public Result<BillVO> selectByDetail(@Valid
                                     @RequestParam(value = "id", required = false)
                                     @NotNull(message = "非法账单") Long id) {
        return Result.success(billService.selectByDetail(id));
    }

}

